package recursion;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.Arrays;

/**
 * 递归解决汉诺塔问题
 *
 * @auth 罗俊华
 * @Date 2022/7/16 11:19 上午
 */
//@Slf4j
public class TowerHanoi {

    private final int A_MAX_INDEX = 3;
    private final int T_MAX_INDEX = 3;
    private final int B_MAX_INDEX = 3;


    private int[] A = new int[A_MAX_INDEX];
    private int[] T = new int[T_MAX_INDEX];
    private int[] B = new int[B_MAX_INDEX];

    private int aIndex = 0;
    private int bIndex = 0;
    private int tIndex = 0;

    public TowerHanoi() {
        for (int i = 0; i < A.length; i++) {

            A[i] = i + 1;
        }
    }

    @Test
    public void move() {

        move(A, T, B, 3 - 1, A_MAX_INDEX - 1, T_MAX_INDEX - 1, B_MAX_INDEX - 1);

        show();
    }

    private int move(int[] from, int[] temp, int[] to, int fromCurrentIndex, int fromMaxIndex, int tempMaxIndex, int toMaxIndex) {

        if (fromCurrentIndex == 0) {

            to[tempMaxIndex] = from[fromCurrentIndex];
            from[fromCurrentIndex] = 0;
            show();
            return fromCurrentIndex;
        } else {
            move(from, temp, to, fromCurrentIndex - 1, fromMaxIndex, tempMaxIndex, toMaxIndex);
        }
//        把这次要移动的A柱上的盘先临时放置在T柱上
        temp[fromCurrentIndex] = from[fromCurrentIndex];
        from[fromCurrentIndex] = 0;
//        把B柱上面已有的盘 放到 T 柱上
        move(to, from, temp, fromCurrentIndex - 1, toMaxIndex, fromMaxIndex, tempMaxIndex);

        show();
        return fromCurrentIndex;
    }

    private void show() {
        System.out.println("-------------开始------------");


        for (int i = 0; i < A_MAX_INDEX; i++) {
            System.out.printf("\t%s\t%s\t%s\n", A[i] == 0 ? "" : String.valueOf(A[i]), T[i] == 0 ? "" : String.valueOf(T[i]), B[i] == 0 ? "" : String.valueOf(B[i]));
        }


        System.out.println("-------------结束------------");

    }

}







